package dao;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import bean.Fund;

public class FundDAO extends DAO<Fund>{

	public FundDAO(SessionFactory factory) {
		super(factory);
	}
	
	public boolean createFund(Fund fund) {
		Transaction tx = null;
		Session session = null;
		try {				
			session = this.getSession();
			tx = session.beginTransaction();
			session.save(fund);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)	tx.rollback();
			e.printStackTrace();
			return false;
		} finally {			
			if(session != null)	session.close();
		}
		return true;
	}
	
	public Fund getFund(int fundId) {
		Session session = null;
		List fund = null;
		try {				
			session = this.getSession();
			Query qr = this.getSession().createQuery("from Fund where id ='"+fundId+"' ");
			fund = qr.list();		
			if (fund == null || fund.size() == 0) return null;
			return (Fund) fund.get(0);
		} catch (HibernateException e) {
			e.printStackTrace();
			return null;
		} finally {
			if(session != null)	session.close();
		}			
	}
	
	public Fund researchFund(String fundName) {
		Transaction tx = null;
		Session session = null;
		List lstFund = null;
		Fund fund = null;
		try {				
			session = this.getSession();
			Query qr = this.getSession().createQuery("from Fund where name like '%"+fundName+"%' OR symbol LIKE '%"+fundName+"%' ");
			lstFund = qr.list();
			if(lstFund.size()>0)
				fund = (Fund) lstFund.get(0);
			
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			if(session != null)	session.close();
		}
		return fund;
	}
	
	public Fund getFundByName(String name) {
		List<Fund> list = this.getByAttribute("name", name, Fund.class);
		if (list == null || list.size() == 0) return null;
		return list.get(0);	
	}
	
	public Fund getFundBySymbol(String symbol) {
		Transaction tx = null;
		Session session = null;
		try {				
			session = this.getSession();
			Query qr = this.getSession().createQuery("from Fund where symbol = '" + symbol + "'");
			List<Fund> funds = qr.list();
			
			if (funds == null || funds.size() == 0) return null;
			return funds.get(0);			
		} catch (HibernateException e) {
			e.printStackTrace();
			return null;
		} finally {
			if(session != null)	session.close();
		}
		
	}
	
	public List getAllFund() {
		Transaction tx = null;
		Session session = null;
		try {						
			session = this.getSession();
			Query qr = this.getSession().createQuery("from Fund");
			System.out.println(qr.getQueryString());

			List<Fund> list = qr.list();
			return list;			
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			if(session != null)	session.close();
		}		
		return null;
	}
}
